package com.orangelife.mobile.myhouse.biz;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.common.jump.WebJumper;
import com.common.sp.UserSp;
import com.curry.android.util.StringUtil;
import com.curry.orangelife.mobile.R;
import com.orangelife.mobile.myhouse.entity.HouseBean;
import com.orangelife.mobile.myhouse.entity.IndustryBean;
import com.orangelife.mobile.myhouse.entity.ResidentBean;
import com.orangelife.mobile.shoppingcart.biz.ShoppingCartBiz;
import com.orangelife.mobile.util.ToastHelper;
import com.orangelife.mobile.zoning.activity.SelectFloorActivity;
import com.orangelife.mobile.zoning.activity.SelectHousesActivity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * 房屋模块的业务逻辑；
 * <p/>
 * Created by 吴格非 on 2015/11/11.
 */
public class HouseBiz {

    public static void jumpBackToWeb(Context context,boolean isOk) {
        HashMap<String, Object> map = new HashMap<>();
        if (isOk) {
            map.put("code", 0);
        } else {
            map.put("code", 1);
        }
        WebJumper.jumpToWebActivity(context, map);
    }

    /** 认证信息 */
    public static String getCertInfo() {
        return UserSp.hasCert() ? "已认证" : "暂未认证，立即认证";
    }

    /*==============VIEW 处理==============*/

    public static void handleConfirmWaitBar(View includeWaitConfirmBar, TextView tvJumpWaitConfirm, HashMap<String, String> map) {
        String count = map.get("count");
        if ("0".equals(count)) {
            includeWaitConfirmBar.setVisibility(View.GONE);
        } else if ("1".equals(count)) {
            includeWaitConfirmBar.setVisibility(View.VISIBLE);
            String certID = map.get("certID");
            String status = map.get("status");
            tvJumpWaitConfirm.setTag(R.string.code_0, certID);
            tvJumpWaitConfirm.setTag(R.string.code_1, status);
            tvJumpWaitConfirm.setText("您有1条认证尚未处理");
        } else {
            includeWaitConfirmBar.setVisibility(View.VISIBLE);
            tvJumpWaitConfirm.setText("您有" + count + "条认证尚未处理");
        }
    }

    public static boolean validateCertInfo(ResidentBean rb) {
        if (rb == null || com.orangelife.mobile.util.StringUtil.isContainBlank(rb.getIdentity(), rb.getCommID())) {
            ToastHelper.getInstance()._toast("对不起，参数不能为空，请检查...");
            return false;
        } else if (com.orangelife.mobile.util.StringUtil.isContainBlank(rb.getCode())) {
            ToastHelper.getInstance()._toast("对不起，验证码不能为空...");
            return false;
        } else if (com.orangelife.mobile.util.StringUtil.isContainBlank(rb.getName())) {
            ToastHelper.getInstance()._toast("对不起，姓名不能为空...");
            return false;
        } else if (com.orangelife.mobile.util.StringUtil.isContainBlank(rb.getPhone())) {
            ToastHelper.getInstance()._toast("对不起，手机号不能为空...");
            return false;
        }
        return true;
    }

    /**
     * @param context
     * @param identityCode 身份0户主，1家属,2租客，3使用人,4保姆 {（0户主，1亲属，2租客）}
     * @return
     */
    public static String getIdentityName(Context context, String identityCode) {
        String str = "";
        switch (identityCode) {
            case HouseBean.ROLE_TYPE_OWNER_CODE:
                str = context.getString(R.string.owner);
                break;
            case HouseBean.ROLE_TYPE_FAMILY_CODE:
                str = context.getString(R.string.family);
                break;
            case HouseBean.ROLE_TYPE_TENANT_CODE:
                str = context.getString(R.string.tenant);
                break;
        }
        return str;
    }

    /**
     * 选择城市
     *
     * @param act
     */
    public static void selectCity(Activity act) {

    }

    /**
     * 选择小区
     */
    public static void selectCommunity() {

    }

    /**
     * 选择楼栋
     *
     * @param act
     * @param commID
     */
    public static void selectBuilding(Activity act, String commID) {
        if (act == null) {
            return;
        }
        Intent intent = new Intent();
        intent.setClass(act,
                SelectFloorActivity.class);
        intent.putExtra("comID", commID);
        act.startActivityForResult(intent, 7);
    }

    /**
     * 选择房号
     *
     * @param act
     * @param buildingID
     */
    public static void selectRoom(Activity act, String buildingID) {
        if (act == null) {
            return;
        }
        if (StringUtil.isContainBlank(buildingID)) {
            ToastHelper.getInstance()._toast("请先选择楼栋");
        } else {
            Intent intent = new Intent();
            intent.setClass(act,
                    SelectHousesActivity.class);
            intent.putExtra("buildingID", buildingID);
            act.startActivityForResult(intent, 8);
        }
    }

    public static boolean selectOne(List<IndustryBean.House> list, int position) {
        boolean isSelectAll;
        boolean isSelectedOne = !(list.get(position).isSelected());//这个判断非常好
        list.get(position).setSelected(isSelectedOne);//单个图标的处理
        isSelectAll = isSelectAll(list);
        return isSelectAll;
    }

    public static boolean selectAll(List<IndustryBean.House> list, boolean isSelectAll, ImageView ivSelectAll) {
        isSelectAll = !isSelectAll;
        ShoppingCartBiz.checkItemHouse(isSelectAll, ivSelectAll);
        for (int i = 0; i < list.size(); i++) {
            list.get(i).setSelected(isSelectAll);
        }
        return isSelectAll;
    }

    private static boolean isSelectAll(List<IndustryBean.House> list) {
        for (int i = 0; i < list.size(); i++) {
            boolean isSelected = list.get(i).isSelected();
            if (!isSelected) {/*如果有一个没选中，就没有全选*/
                return false;
            }
        }
        return true;
    }

    /**
     * @param ib 如果有停车场，那么列表第一项必然是停车场，需要特殊处理
     * @return
     */
    public static IndustryBean getSelectedCommitInfo(IndustryBean ib) {
        if (ib == null) {
            return null;
        }
        boolean hasParkingSpace = IndustryBean.HAS_PARKING_SPACE.equals(ib.getHasParkingSpace());
        List<IndustryBean.House> list = ib.getHouseList();
        ArrayList<IndustryBean.House> temp = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            if (i == 0 && hasParkingSpace) {//如果有停车位，则处理第一个数据
                if (list.get(0).isSelected()) {
                    ib.setHasParkingSpace(IndustryBean.HAS_PARKING_SPACE);
                } else {
                    ib.setHasParkingSpace(IndustryBean.NO_PARKING_SPACE);
                }
            } else if (list.get(i).isSelected()) {
                temp.add(list.get(i));
            }
        }
        ib.setHouseList(temp);
        return ib;
    }

    public static boolean isSelectedNothing(List<IndustryBean.House> list) {
        boolean isSelectedNothing = true;
        if (list == null) {
            return isSelectedNothing;
        }
        for (int i = 0; i < list.size(); i++) {
            if (list.get(i).isSelected()) {
                isSelectedNothing = false;
                break;
            }
        }
        return isSelectedNothing;
    }

}
